package com.songcha.module_bookreader.helper

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.songcha.module_bookreader.service.BookDownloadService
import com.songcha.library_business.bean.book.BookBean
import com.songcha.library_business.bean.book.BookChapterCatalogBean
import com.songcha.library_business.provider.IBookDownloadProvider
import com.songcha.library_common.LibraryCommon

class MyServiceConnection(val book:BookBean,val chapterList:MutableList<BookChapterCatalogBean.DataBean>):ServiceConnection{
    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        if(service==null) return

        (service as BookDownloadService.MyBinder).startDownload(book,chapterList)
    }

    override fun onServiceDisconnected(name: ComponentName?) {

    }

}

@Route(path = "/bookreader/BookDownProviderImpl")
class BookDownloadHelper : IBookDownloadProvider {
    companion object{
        var conn:MyServiceConnection?=null

        @JvmStatic
        fun startBookDownloadService(book:BookBean,chapterList:MutableList<BookChapterCatalogBean.DataBean>){
            val intent=Intent(LibraryCommon.getContext(), BookDownloadService::class.java)
            conn=MyServiceConnection(book,chapterList)
            LibraryCommon.getContext().bindService(intent,conn!!,AppCompatActivity.BIND_AUTO_CREATE)
        }
    }

    override fun startDownload(
        book: BookBean,
        chapterList: MutableList<BookChapterCatalogBean.DataBean>
    ) {
        startBookDownloadService(book,chapterList)
    }

    override fun init(context: Context?) {

    }


}